#include "inputform.h"
#include "ui_inputform.h"
#include <QDebug>

inputForm::inputForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::inputForm)
{
    ui->setupUi(this);

    ui->addfileButton->setEnabled(false);
    ui->pushButton->setEnabled(false);


    
}

inputForm::~inputForm()
{
    delete ui;
}

void inputForm::on_pushButton_clicked()
{   
    emit infoSignal(ui->nameEdit->text(),ui->priceEdit->text().toInt(),
                    ui->usetimeEdit->text().toInt(),ui->costEdit->text().toInt(),filenameUsed);

    close();
}

void inputForm::on_addfileButton_clicked()
{
    qDebug()<<urs[0].toLocalFile();    //拖入文件的本地绝对路径

    QString fileName;
    QFileInfo fileinfo(urs[0].toLocalFile());
    fileName = fileinfo.fileName();
    qDebug()<<fileName;    //新增图片名+后缀名

    filenameUsed = QString("./Smart_menus/pic/%1").arg(fileName);    //拼接后带有新图片名字加后缀名的文件保存路径

    bool ok = QFile::copy(urs[0].toLocalFile(),filenameUsed);
    if(ok == true)
    {
        qDebug()<<"拷贝成功";
    }
    else
    {
        qDebug()<<"拷贝失败";
    }

}

void inputForm::dragEnterEvent(QDragEnterEvent *event)
{
    //如果文件类型为jpg或png才接受拖动行为
    //功能类似于compare字符串比较函数
    if(!event->mimeData()->urls()[0].fileName().right(3).compare("jpg")
            || !event->mimeData()->urls()[0].fileName().right(3).compare("png")
            || !event->mimeData()->urls()[0].fileName().right(3).compare("gif"))
    {
        event->acceptProposedAction();//接受事件
    }
    else
    {
        event->ignore();//拒绝事件
    }
}

void inputForm::dropEvent(QDropEvent *event)
{
    mi = event->mimeData();    //获取mimeData
    QPixmap pic(mi->urls()[0].toLocalFile());    //toLocalFile()是获取拖动文件的本地路径
    urs = mi->urls();
    qDebug()<<mi->urls()[0].toLocalFile();    //文件本地绝对路径
    qDebug()<<mi->urls().size();
    ui->picLabel->resize(pic.width(),pic.height());
    ui->picLabel->setPixmap(pic);

    ui->addfileButton->setEnabled(true);
}

void inputForm::on_nameEdit_textChanged(const QString &arg1)
{
    if(arg1 != "")
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}
